package com.airticketbooking.controller.user;

import com.airticketbooking.common.domain.PageDTO;
import com.airticketbooking.common.domain.Result;
import com.airticketbooking.query.FlightSchedulePageQuery;
import com.airticketbooking.vo.FlightScheduleVO;
import com.airticketbooking.service.ScheduleService;
import com.github.pagehelper.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


@RequestMapping("air/user/Schedule")
@RestController
@Api(tags = "用户端班次信息查询接口")
@RequiredArgsConstructor
@Slf4j
public class UScheduleController {


	private final ScheduleService shceduleService;


	@ApiOperation("分页获取所有班次信息")
	@GetMapping("/page")
	public Result<PageDTO<FlightScheduleVO>> list(FlightSchedulePageQuery flightSchedulePageQuery) throws Exception {

		log.info("分页获取所有合适的班次信息查询:{}", flightSchedulePageQuery);
		try {
			Page<FlightScheduleVO> flightPageinfo = shceduleService.list(flightSchedulePageQuery);

			PageDTO<FlightScheduleVO> pageDTO = new PageDTO<>();
			pageDTO.setTotal(flightPageinfo.getTotal());
			pageDTO.setList(flightPageinfo.getResult());

			return Result.ok(pageDTO);
		} catch (Exception e) {

			return Result.error(e.getMessage());
		}
	}

	//	根据id查询班次信息
	@ApiOperation("根据id查询班次信息")
	@GetMapping("/{id}")
	public Result<FlightScheduleVO> getById(@PathVariable("id") int id) throws Exception {

		log.info("查询班次信息id:{}", id);
		FlightScheduleVO flightScheduleVO = shceduleService.getShceduleVOById(id);

		log.info("get flight by id Result:{}", flightScheduleVO);
		return Result.ok(flightScheduleVO);
	}

	//	查询所有航班信息
	@ApiOperation("查询所有航班信息")
	@GetMapping("/all")
	public Result<PageDTO<FlightScheduleVO>> getAll() throws Exception {

		log.info("查询所有航班信息");
		Page<FlightScheduleVO> flightPageinfo = shceduleService.getAll();

		PageDTO<FlightScheduleVO> pageDTO = new PageDTO<>();
		pageDTO.setTotal(flightPageinfo.getTotal());
		pageDTO.setList(flightPageinfo.getResult());

		return Result.ok(pageDTO);
	}

}
